package com.wintop.youyuan.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.wintop.youyuan.dao.MailBoxDao;
import com.wintop.youyuan.model.Inform;
import com.wintop.youyuan.model.Luck;
import com.wintop.youyuan.model.Message;

@Repository("mailBoxDao")
public class MailBoxDaoImpl implements MailBoxDao {
	private SessionFactory sessionFactory;

	@Resource(name = "sessionFactory")
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public boolean addMailInformation(Object object) {
		Session session = sessionFactory.getCurrentSession();
		session.save(object);
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Luck> getLucks(String userid, String property) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session
				.createQuery("from Luck where userid=:id and property=:pro");
		query.setString("id", userid);
		query.setString("pro", property);
		return query.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Message> getMessages(String userid) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session
				.createQuery("from Message where receiverUser=:id");
		query.setString("id", userid);
		return query.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Message> getMessages(String userid, String sendId) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session
				.createQuery("from Message where (receiverUser=:id and sendUser=:sendid) or (receiverUser=:sendid and sendUser=:id)");
		query.setString("id", userid);
		query.setString("sendid", sendId);
		return query.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Inform> getInforms(String userid) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Inform where userid=:id");
		query.setString("id", userid);
		return query.list();
	}

	@Override
	public boolean deleteMailInformation(Object object) {
		Session session = sessionFactory.getCurrentSession();
		session.delete(object);
		return true;
	}

	@Override
	public int getMessageNum(String userid) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session
				.createQuery("select count(*) from Message where receiverUser=:id and isRead=0");
		query.setString("id", userid);
		int count = ((Long) query.iterate().next()).intValue();
		// return (int) query.uniqueResult();
		return count;
	}

	@Override
	public int getInformNum(String userid) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session
				.createQuery("select count(*) from Inform where userid=:id and isRead=0");
		query.setString("id", userid);
		int count = ((Long) query.iterate().next()).intValue();
		return count;
	}

	@Override
	public boolean readMessage(String userid, String sendId) {
		Session session = sessionFactory.getCurrentSession();
		String hq = "update Message set isRead = 1 where receiverUser=:id and sendUser=:sendid and isRead=0";
		Query query = session.createQuery(hq);
		query.setString("id", userid);
		query.setString("sendid", sendId);
		int a = query.executeUpdate();
		return a > 0;
	}

	@Override
	public boolean deleteMessages(String userid, String sendId) {
		Session session = sessionFactory.getCurrentSession();
		String hq = "delete Message where receiverUser=:id and sendUser=:sendid";
		Query query = session.createQuery(hq);
		query.setString("id", userid);
		query.setString("sendid", sendId);
		int a = query.executeUpdate();
		return a > 0;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Message> getRecentMessages(String userid) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Message where sendUser=:id");
		query.setString("id", userid);
		return query.list();
	}

	@Override
	public Luck getAttention(String userid, String sendId) {
		Session session = sessionFactory.getCurrentSession();
		String hql = "from Luck where userid=:uid and property='myAttention' and value1=:sid";
		Query q = session.createQuery(hql);
		q.setString("uid", userid);
		q.setString("sid", sendId);
		Luck luck = (Luck) q.uniqueResult();
		return luck;
	}

}
